<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
		<meta name="author" content="wangyuguo">
		<title>组织机构管理</title>
		<script type="text/javascript" language="JavaScript" src="../script/common.js"></script>
		<script type="text/javascript" language="JavaScript" src="../script/tree.js"></script>
		<script type="text/javascript" language="JavaScript">
			var tree;
			var qt = null;
			var org = null;
			var openIcon = "../images/tree/root.png";
			var icon = "../images/tree/root.png";
			var orgLevelMap = {};				//用以记录各个等级对应的下级级别 如大区200的下级级别为 大区200，地市500，站级700
			
			var isHq = false;
			
			function load() {
				Eui.showNavigation();//导航
				setPages(["showview","detview","listview"]);
				org = Common.cOrg();
				if (org.orgLevel == Constants.ORG_LEVEL_HQ){
					isHq = true;
					$("treeAddDiv").style.display = "";
					$("treeBtnDiv").style.display = "";
				}else{
					CardMgnt.showViewForm("treeform");
				}
				var orgLevels = SystemMgnt.orderOrgLevel(true);
				orgLevelMap["root"] = orgLevels;
				for (var i = 0; i < orgLevels.length; i++) {
					var o = orgLevels[i];
					orgLevelMap[o.code] = orgLevels.slice(i + 1);
				}
				Common.initDict(orgform.orgLevel, "org_level", "请选择");
				Common.initDict(treeform.orgLevel, "org_level", "请选择");
			
				showTree();
			}
			
			
			/**查询部分**/
			//生成table
			function createTable() {
				var columns = []
				columns[0] = {type:"checkbox"};
				columns[1] = {render:orgName};
				if (isHq)
					columns[7] = {title:"操作",width:15,order:false,render:function(v, r) {
						return "<a href=# onclick=\"onEdit(" + r[0] + ",'orgform');return false;\">编辑</a>";
					}};
				var buttons = [];
				if (isHq)
					buttons = [
						{value:" 增 加 ",actions:function(){onNew(orgform)}},
						{value:" 删 除 ",actions:onDelete}
					]
				qt = new QueryTable("organ", "searchForm", "datalist", columns, {
					buttons:buttons
				});
				qt.paramFunc = function() {
					var params = Common.getValues(searchForm);
					if (org.orgLevel != Constants.ORG_LEVEL_HQ) {
						params.orgLevel = org.orgLevel;
						params.orgId = org.id;
					} 
					return params;
				}
			}
			//数据处理
			function orgName(v, r) {
				return "<a href=# onclick=\"showView(" + r[0] + ");return false;\">" + v + "</a>";
			}
			
			function showView(id) {
				SystemMgnt.showOrganization("showview",id);
				switchPage("showview");
			}
			
			/**树**/
			function writeTree() {
				if (tree)
					webTreeHandler.init();
				tree = new LoadTree(org.name, org.id, "javascript:showProperty()", null, "../images/tree/root.png", "../images/tree/root.png");
				tree.obj = org;
				$("treeDiv").innerHTML = tree;
				treeform.reset();
			}
			function _startLoadXmlTree(sSrc, jsNode, newNode) {
				DWR.call("partyService.findOrgByParent", sSrc, function(result) {
					_xmlFileLoaded(result, jsNode);
				});
			}
			
			function _createNode(obj) {
				if (obj.id)
					node = createTreeNode(obj.orgLevel == Constants.ORG_LEVEL_STATION, obj.name, obj.id, "javascript:showProperty()");
				else
					node = createTreeNode(true, "新机构", null, "javascript:showProperty()");
				node.obj = obj;
				return node;
			}
			
			function showProperty() {
				var node = tree.getSelected();
				treeform.treeNodeId = node.id;
				if (node.obj.id)
					formDisable(true);
				else
					formDisable(false);
				onEdit(node.obj.id, 'treeform');
			}
			/**删除组织机构(列表)**/
			function onDelete() {
	    		var ids = qt.getBoxValue();
				Eui.onDel("partyService.deleteOrgans", ids, "组织机构", null, function(){qt.showTable();});
			}
			/**删除组织机构(树)**/
			function treeRemove() {
				var node = tree.getSelected();
				if (!node) {
					showInformation("请选择需要删除的组织机构");
					return;
				}
				var sel = function(){
					node.remove();
					showProperty();
				}
				if (node.obj.id) {
					if (showQuestion("是否删除组织机构？")) {
						DWR.call("partyService.deleteOrgans", node.obj.id, function() {
							showInformation("机构【"+node.obj.name+"】删除成功");
							sel();
						});
					}
				} else {
					sel();
				}
			}
			
			/**组织机构保存**/
			function onSave(form) {
				if (!checkForm(form))return;
				var organ = Common.getValues(form);
				organ.isGiftLocation = form.isGiftLocation.checked;
				organ.isDeposit = form.isDeposit.checked;
				organ.isStation = form.isStation.checked;
				DWR.call("partyService.saveOrganization", organ, function(r) {
					showInformation("组织机构【"+organ.name+"】保存成功");
					if (form == orgform) {
						switchPage('listview');
						if (r)
							qt.showTable();
					} else {
						var treeNode = webTreeHandler.all[treeform.treeNodeId];
						if (treeNode) {
							treeNode.obj = r;
							treeNode.setText(r.name);
							treeNode.isLeaf = r.orgLevel == "700";
							treeNode.src = r.id;
						}
						if (!organ.id && !treeNode.isLeaf)
							treeNode.fileToFolder();
						showProperty();
					}
				},{blockScreen:true})
			}
			/**组织机构明细**/
			//新建组织机构
			function onNew(form) {
				if (form == orgform) {
					form.reset();
					form.parent.value = org.id;
					form.parentname.value = org.name;
					switchPage("detview");
				} else {
					var parentNode = tree;
					if (tree.getSelected()) {
						parentNode = tree.getSelected();
					}else{
						showWarning("请选择一个机构，然后增加下级机构");
						return;
					}
					if (parentNode.obj.id == null) {
						showError("上级机构没有保存");
						return;
					}
					var list = orgLevelMap[parentNode.obj.orgLevel];
					Common.initDict(treeform.orgLevel, list,"请选择");
					newNodeToTree(parentNode,{childAddText:"不能为站级机构添加下级机构"});
				}
			}
			//组织机构修改
			function onEdit(id, formName) {
				var form = $(formName);
				if (id) {
					DWR.call("partyService.getOrganization", id, function(result) {
						var org = result.organization;
						org.parentname = result.parentName;
						org.checklevel = result.parentLevel;
						var list = orgLevelMap[result.parentLevel];
						if (!list)
							list = orgLevelMap['root'];
						Common.initDict(orgform.orgLevel, list);
						Common.initDict(treeform.orgLevel, list);
						Common.setValues(form, org);
						form.isDeposit.value = org.isDeposit;
						form.isDeposit.checked = org.isDeposit;
						form.isStation.value = org.isStation;
						form.isStation.checked = org.isStation;
						form.isGiftLocation.value = org.isGiftLocation;
						form.isGiftLocation.checked = org.isGiftLocation;
						_checkParent(org, formName);
						if (form == orgform) {
							switchPage("detview");
						}
					});
				} else {
					var p = tree.getSelected().parentNode;
					form.reset();
					form.parent.value = p.src;
					form.parentname.value = p.text;
					form.checklevel.value = p.obj.orgLevel;
				}
			}
			function _checkParent(org, formName) {
				var form = $(formName);
				if (!org.parent) {
					form.parentname.onblur = function() {
					}
					$(formName + 'span').style.display = "none";
				} else {
					form.parentname.onblur = function() {
						checkV(this, '', true);
					}
					$(formName + 'span').style.display = "";
				}
			}
			//选择上级机构
			function setOrg(sorg) {
				orgform.parent.value = sorg.id;
				orgform.parentname.value = sorg.name;
				Common.initDict(orgform.orgLevel, orgLevelMap[sorg.orgLevel])
			}
			
			/**其他方法**/
			//列表模式
			function showList() {
				$('treemode').style.display = "none";
				$("treemode1").style.display = "none";
				$('listmode').style.display = "";
				if (qt)
					qt.showTable();
				else
					createTable();
			}
			//树状模式
			function showTree() {
				$('treemode').style.display = "";
				$("treemode1").style.display = "";
				$('listmode').style.display = "none";
				writeTree();
			}
			function formDisable(status) {
				for (var i=0;i<treeform.elements.length;i++){
					var t = treeform.elements[i];
					if (!t.readOnly)
						t.disabled = status;
				}
				treeform.editbt.disabled = !status;
			}
			
			function onCancel(pageId) {
				if (pageId == 'listview') {
					CardMgnt.resetForm(orgform)
					switchPage('listview');
				} else {
					var node = tree.getSelected()
					if (!node.obj.id) {
						node.remove();
					}
					showProperty();
					CardMgnt.resetForm(treeform)
				}
			}
			
			function onEditOrg() {
				formDisable(false);
			}
			
			function datePicker() {
				if (treeform.invalidTime.disabled) return;
				showDatePicker(treeform.invalidTime);
			}
		</script>
	</head>
	
	<body onload="load()" class="noScroll">
<table cellSpacing=0 cellPadding=0 width="100%" border=0 height="100%">
    <tr>
        <td class="nav" colspan="2">
			<table width="100%" cellspacing="0" cellpadding="0" border="0">
				<tr>
					<td width="70%">
						<div id="nav">&nbsp;</div>
					</td>
					<td align="right" width="30%">
						<a href="#" onclick="showTree()">树状模式</a> 
						<!--<a href="#" onclick="showList()">列表模式</a>-->
					</td>
				</tr>
			</table>
        </td>
	</tr>
	<tr id="listmode" style="height:100%">
		<td align="center" valign="top" colspan="2">
			<div id="listview">
				<div class="searchForm">
					<form id="searchForm" name="searchForm">
						<table cellSpacing="0" cellPadding="4" width="100%" border="0">
							<tr>
								<td>
									<label>上级机构</label>
									<input type="text" name="parentName" maxlength="20">
								</td>
								<td>
									<label>机构名称</label> 
									<input type="text" name="name" maxlength="20">
								</td>
								<td>
									<label>机构编码</label>
									<input type="text" name="uniqueId" onblur="checkV(this,'half')" maxlength="4">
								</td>
								<td><button type="button" onclick="qt.showTable()">查 询</button></td>
							</tr>
						</table>
					</form>
				</div>
				<div id="datalist"></div>
			</div>
			<div id="showview" style="display:none;"></div>
			<div id="detview" style="display:none;">
				<form id="orgform" name="orgform">
					<input type="hidden" name="id">
					<div class="form">
						<div class="formTitle">编辑机构信息</div>
						<table cellSpacing="0" cellPadding="4" width="100%" border="0" class="label5">
							<tr>
								<td>
									<label>上级机构</label> 
									<input type="text" name="parentname" readonly class="readonly" onblur="checkV(this,'',true)"><a class="orga" href="#" onclick="SystemMgnt.selOrg(setOrg)">&nbsp;&nbsp;</a><span class="des" id="orgformspan">*</span>
								</td>
								<td>
									<label>机构级别</label> 
									<select name="orgLevel" onblur="checkV(this,'',true)"></select>
									<span class="des">*</span>
									<input type="hidden" name="checklevel">
									<input type="hidden" name="parent">
								</td>
							</tr>
							<tr>
								<td>
									<label>机构名称</label> 
									<input type="text" name="name" maxlength="20" onblur="checkV(this,'',true)">
									<span class="des">*</span>
								</td>
								<td>
									<label>机构编码</label>
									<input type="text" name="uniqueId" onblur="checkV(this,'half',true)" maxlength="4">
									<span class="des">*</span>
								</td>
							</tr>
							<!-- 
							<tr>
								<td>
									<label>注册时间</label>
									<input type="text" name="registerTime" readonly class="readonly" onblur="checkV(this,'date');">
								</td>
								<td>
									<label>有效时间</label> 
									<input type="text" name="invalidTime" onblur="checkV(this,'futureDate')" readonly class="readonly"><a href="javascript:void(0)" onclick="showDatePicker( orgform.invalidTime );" class="date" title="选择日期">&nbsp;&nbsp;</a>
								</td>
							</tr>
							 -->
							<tr>
								<td>
									<label>联系电话</label>
									<input type="text" name="phone" maxlength="20" onblur="checkV(this,'phone')">
								</td>
								<td>
									<label>联系地址</label>
									<input type="text" name="address" maxlength="50">
								</td>
							</tr>
							<tr>
								<td>
									<label>价格基数</label>
									<input type="text" name="description" maxlength="3" onblur="checkV(this,'double',true)">%
									<span class="des">*</span>
								</td>
							</tr>
							<tr>
								<td>
									<label>内部机构</label>
									&nbsp;&nbsp;&nbsp;是<input type="radio" name="isInternal" value="true">
									&nbsp;&nbsp;&nbsp;否<input type="radio" name="isInternal" value="false">
								</td>
								<td>
									<div style="display:none;">
									<label>站点属性</label>
									<input type="checkbox" name="isStation">油站
									<input type="checkbox" name="isDeposit">充值
									<input type="checkbox" name="isGiftLocation">兑奖
									</div>
								</td>
							</tr>
							<!-- tr>
								<td colspan="2">
									<label>保证金帐户</label>
									&nbsp;&nbsp;&nbsp;是<input type="radio" name="useCreditDeposit" value="true">
									&nbsp;&nbsp;&nbsp;否<input type="radio" name="useCreditDeposit" value="false">
								</td>
							</tr -->
						</table>
					</div>
					<div align="center">
						<button name="savebt" onclick="onSave(orgform)">保 存</button>
						<button onclick="onCancel('listview')">取 消</button>
					</div>
				</form>
			</div>
		</td>
		</tr>
		<tr id="treemode" style="display:none;" class="frameBody">
			<td width="29%" class="treeBtn treeBar">
				<div id="treeAddDiv" style="display:none;">
					<a title="增加" href="javascript:void(0)" onclick="onNew(treeform)"><img alt="" src="../images/button/add.gif" border="0" align="absmiddle"> 增 加</a>
					<a title="删除" href="javascript:void(0)" onclick="treeRemove()"><img alt="" src="../images/button/del.gif" border="0" align="absmiddle"> 删 除</a>
				</div>
			</td>
			<td width="68%" class="treeBar" valign=bottom>
				<a href="#" onfocus="this.blur()" tabindex="-1" id="tab_base" class="treeTabc">基本属性</a>
			</td>
		</tr>
		<tr class="treeBody" id="treemode1">
			<td valign="top" width="29%">
				<div class="scrollDiv">
					<div id="treeDiv" class="treeDiv"></div>
				</div>
			</td>
			<td valign="top" align="center" width="68%">
				<div id="treeDet" class="scrollDiv">
					<form id="treeform" name="treeform">
					<input type="hidden" name="id">
					<input type="hidden" name="checklevel">
					<input type="hidden" name="parent">
					<div class="form">
						<div class="formTitle">机构详细信息</div>
						<table cellSpacing="0" cellPadding="4" width="100%" border="0" class="col2 label5">
							<tr>
								<td width="50%">
									<label>上级机构</label>
									<input type="text" name="parentname" readonly class="readonly" onblur="checkV(this,'',true)">
									<span id="treeformspan" class="des">*</span>
								</td>
								<td width="50%">
									<label>机构级别</label>
									<select name="orgLevel" onblur="checkV(this,'',true)" disabled></select>
									<span class="des">*</span>
								</td>
							</tr>
							<tr>
								<td>
									<label>机构名称</label>
									<input type="text" name="name" maxlength="20" onblur="checkV(this,'',true)" disabled>
									<span class="des">*</span>
								</td>
								<td>
									<label>机构编码</label>
									<input type="text" name="uniqueId" onblur="checkV(this,'half',true)" maxlength="4" disabled>
									<span class="des">*</span>
								</td>
							</tr>
							<!-- 
							<tr>
								<td>
									<label>注册时间</label>
									<input type="text" name="registerTime"  readonly class="readonly" onblur="checkV(this,'date');">
								</td>
								<td>
									<label>有效时间</label>
									<input type="text" name="invalidTime" onblur="checkV(this,'futureDate')"  readonly class="readonly"><a href="javascript:void(0)" onclick="showDatePicker( treeform.invalidTime );" class="date" title="选择日期">&nbsp;&nbsp;</a>
								</td>
							</tr> -->
							<tr>
								<td>
									<label>联系电话</label>
									<input type="text" name="phone" maxlength="20" disabled onblur="checkV(this,'phone')">
								</td>
								<td>
									<label>联系地址</label>
									<input type="text" name="address" maxlength="50" disabled>
								</td>
							</tr>
							<tr>
								<td>
									<label>价格基数</label>
									<input type="text" name="description" maxlength="3" disabled onblur="checkV(this,'double',true)">%
									<span class="des">*</span>
								</td>
							</tr>
							<tr>
								<td>
									<label>内部机构</label>
									&nbsp;&nbsp;<input type="radio" name="isInternal" value="true">&nbsp;是
									&nbsp;&nbsp;<input type="radio" name="isInternal" value="false">&nbsp;否
								</td>
								<td>
									<div style="display:none;">
									<label>站点属性</label>
									<input type="checkbox" name="isStation">油站
									<input type="checkbox" name="isDeposit">充值
									<input type="checkbox" name="isGiftLocation">兑奖
									</div>
								</td>
							</tr>
							<!-- tr>
								<td colspan="2">
									<label>保证金帐户</label>
									&nbsp;&nbsp;<input type="radio" name="useCreditDeposit" value="true">&nbsp;是
									&nbsp;&nbsp;<input type="radio" name="useCreditDeposit" value="false">&nbsp;否
								</td>
							</tr -->
						</table>
					</div>
					<p id="treeBtnDiv" style="display:none;">
						<button type="button" name="editbt" onclick="onEditOrg();" disabled>编 辑</button>
						<button type="button" name="savebt" onclick="onSave(treeform);" disabled>保 存</button>
						<button type="button" name="cancelbt" onclick="onCancel()" disabled>取 消</button>
					</p>
				</form>
				</div>
			</td>
		</tr>
		</table>
	</body>
</html>